package com.yisu.auction.manager.core.service.impl;


import com.yisu.auction.ewf.common.web.EasyUIGrid;
import com.yisu.auction.ewf.entity.Advertisement;
import com.yisu.auction.manager.core.service.IAdvertisementService;
import com.yisu.auction.manager.core.mapper.AdvertisementMapper;
import com.yisu.auction.manager.domain.request.baseInfoPackage.AdvertisementRequest;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：广告位业务接口实现类
 * createTime：2017/8/10
 */
@Service("IAdvertisementService")
public class AdvertisementServiceImpl extends BaseService implements IAdvertisementService {
    @Resource
    private AdvertisementMapper advertisementMapper;

    /**
     * author:赵杰
     * Business Description : 通过主键ID，获取广告位信息
     *
     * @param primaryId 主键信息
     * @return
     */
    @Override
    public Advertisement getAdvertisementModelByPrimaryId(long primaryId) {
        return advertisementMapper.selectByPrimaryKey(primaryId);
    }

    /**
     * author:赵杰
     * Business Description : 通过主键ID，删除广告位信息
     *
     * @param primaryId 主键信息
     * @return
     */
    @Override
    public boolean deleteAdvertisementByPrimaryId(long primaryId) {
        return advertisementMapper.deleteByPrimaryKey(primaryId) > 0;
    }

    /**
     * author:赵杰
     * Business Description : 编辑广告位信息
     *
     * @param advertisement
     * @return
     */
    @Override
    public boolean modifyAdvertisement(Advertisement advertisement) {
        if (StringUtils.isEmpty(advertisement.getAppSign())) {
            return false;
        }
        List<String> appSigns = Linq4j.asEnumerable(advertisement.getAppSign().split(",")).toList();
        if (appSigns.size() > 1) {
            advertisementMapper.deleteByName(advertisement.getName());
            for (String item : appSigns) {
                Advertisement insertModel = new Advertisement();
                insertModel.setCreateDate(new Date());
                insertModel.setIsValid(true);
                insertModel.setUpdateDate(new Date());
                insertModel.setSort(advertisement.getSort());
                insertModel.setName(advertisement.getName());
                insertModel.setProductId(advertisement.getProductId());
                insertModel.setImage(advertisement.getImage());
                insertModel.setUrl(advertisement.getUrl());
                insertModel.setAppSign(item.trim());
                advertisementMapper.insertSelective(insertModel);
            }
            return true;
        } else {
            Advertisement checkModel = advertisementMapper.selectByPrimaryKey(advertisement.getId());
            if (checkModel == null) {
                advertisement.setCreateDate(new Date());
                advertisement.setIsValid(true);
                return advertisementMapper.insertSelective(advertisement) > 0;
            } else {
                Advertisement updateModel = new Advertisement();
                updateModel.setUpdateDate(new Date());
                updateModel.setSort(advertisement.getSort());
                updateModel.setName(advertisement.getName());
                updateModel.setProductId(advertisement.getProductId());
                updateModel.setImage(advertisement.getImage());
                updateModel.setUrl(advertisement.getUrl());
                updateModel.setAppSign(advertisement.getAppSign().trim());
                updateModel.setId(advertisement.getId());
                return advertisementMapper.updateByPrimaryKeySelective(updateModel) > 0;
            }
        }
    }

    /**
     * author:赵杰
     * Business Description : 分页获取广告位列表
     *
     * @param advertisementRequest 分页请求信息
     * @return 广告位列表信息
     */
    @Override
    public EasyUIGrid<Advertisement> getAdvertisementByPage(AdvertisementRequest advertisementRequest) {
        List<Advertisement> advertisements = advertisementMapper.getAdvertisementByPage(advertisementRequest);
        EasyUIGrid<Advertisement> gridData = new EasyUIGrid<>();
        gridData.setTotal(advertisementRequest.getTotalRecords());
        gridData.setRows(advertisements);
        return gridData;
    }

    /**
     * author:黄萧林
     * Business Description:获取排序最大值+1
     *
     * @return
     */
    @Override
    public int getMaxSort() {
        return advertisementMapper.getMaxSort();
    }
}
